package com.yukexx.common.utils;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;



/**
 * <p>Title: UserRealm.java</p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2017</p>
 * <p>Company: www.yukexx.com</p>
 * @author 王磊
 * @date 2018年6月7日 17:28:53
 * @version 1.0
 */
public class UserRealm extends  AuthorizingRealm {
//	@Autowired
//	private UserService userService;
	/* (non-Javadoc)
	 * 进行授权
	 * @see org.apache.shiro.realm.AuthorizingRealm#doGetAuthorizationInfo(org.apache.shiro.subject.PrincipalCollection)
	 */
	@Override
	protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
		// TODO Auto-generated method stub
		SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
		//获取用户实体 即用户名
		Object primaryPrincipal = principalCollection.getPrimaryPrincipal();
		//String role = userService.getRolesByUserName(primaryPrincipal.toString());
		String role = "";
		System.out.println("当前用户权限:"+role);
		info.addRole(role);
		return info;
	}

	/* (non-Javadoc)
	 * 用户登录认证
	 * @see org.apache.shiro.realm.AuthenticatingRealm#doGetAuthenticationInfo(org.apache.shiro.authc.AuthenticationToken)
	 */
	@Override
	protected AuthenticationInfo doGetAuthenticationInfo(
			AuthenticationToken authenticationToken) throws AuthenticationException {
		// TODO Auto-generated method stub
		UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken;
		String username = token.getUsername();
		System.out.println("username:" + username);
		Object principal = username;
		Object credentials="4280d89a5a03f812751f504cc10ee8a5";
		
		SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(principal, credentials,null,getName());
		/*if(userService.getUserByPhone(username) == null)
			throw new UnknownAccountException();//没找到帐号
		//获取登录实体
		Object principal=username;
		System.out.println("username" + username);
		//从数据库中获取登录凭证
		Object credentials=userService.getUserByPhone(username).getPassword();
		}*/
		return info;
			
		
		
	}
	
	

}
